//
//  LXSelDateField.m
//  ibgs_patients
//
//  Created by xiao.zhao@lachesis-mh.com on 16/1/6.
//  Copyright © 2016年 xiao.zhao@lachesis-mh.com. All rights reserved.
//

#import "LXSelDateField.h"
#import "LXTimePickerView.h"
#import "LXFieldAccessView.h"
#import "LXDatePickerView.h"

@interface LXSelDateField () <LXFieldAccessViewDelegate>
@property (nonatomic, strong) LXTimePickerView *timePicker;
@property (nonatomic, strong) LXDatePickerView *datePicker;
@end

@implementation LXSelDateField

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        [self setup];
    }
    
    return self;
}

-(void)setup
{
    self.font = [UIFont lx_systemFontOfSize:20.0f];
    self.textColor = kAppGreenTextColor;
    
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    _timePicker = [[LXTimePickerView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, 180.0f)];
    _datePicker = [[LXDatePickerView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 180.0f)];
    LXFieldAccessView *accessView = [[LXFieldAccessView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, 50.0f)];
    accessView.textLabel.text = @"记录时间";
    accessView.delegate = self;
    
    self.inputAccessoryView = accessView;
}

-(void)setSelectedDate:(NSDate *)selectedDate
{
    if(_selectedDate != selectedDate)
    {
        _selectedDate = selectedDate;
        self.text = [self getFormateStringWithDate:_selectedDate];
    }
    _datePicker.selectedDate = selectedDate;
    _timePicker.selectedDate = selectedDate;
}

-(void)setNoShowTime:(BOOL)noShowTime
{
    _noShowTime = noShowTime;
    if (_noShowTime) {
        self.inputView = _datePicker;
    } else {
        self.inputView = _timePicker;
    }
}

#pragma mark -LXFieldAccessViewDelegate
-(void)accessView:(LXFieldAccessView *)accessView didClickWithIndex:(NSInteger)index
{
    if(index == 1)
    {
        //取消
    }
    else if (index == 2)
    {
        //完成
        NSDate *selDate;
        if (_noShowTime) {
            LXDatePickerView *datePicker = (LXDatePickerView *)self.inputView;
            selDate = datePicker.selectedDate;
        } else {
            LXTimePickerView *timePicker = (LXTimePickerView *)self.inputView;
            selDate = timePicker.selectedDate;
        }
        
        self.selectedDate = selDate;
        
        if (_dateDelegate && [_dateDelegate respondsToSelector:@selector(LXSelDateField:newDate:)]) {
            [_dateDelegate LXSelDateField:self newDate:selDate];
        }
    }
    
    [self resignFirstResponder];
}

-(NSString *)getFormateStringWithDate:(NSDate *)date
{
    if(!date)
    {
        return nil;
    }
    NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:date];
    
    NSArray *weekdays = @[@"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六"];
    
    NSString *formateStr;
    if (_noShowTime) {
        formateStr = [NSString stringWithFormat:@"%02ld/%02ld/%04ld", dateComponents.day, dateComponents.month, dateComponents.year];
    } else {
        formateStr = [NSString stringWithFormat:@"%@ %02ld:%02ld %02ld/%02ld", weekdays[dateComponents.weekday - 1], dateComponents.hour, dateComponents.minute, dateComponents.month, dateComponents.day];
    }
    return formateStr;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
